//
//  ViewControllerHelp.m
//  lvfj
//
//  Created by lvfeijun on 2022/5/12.
//  Copyright © 2022 Goudu KuaiEx Technology(Shenzhen)Co.,ltd. All rights reserved.
//

#import "ViewControllerHelp.h"

@implementation ViewControllerHelp
//获取根视图
+ (UIViewController *)currentWidowsViewController
{    UIViewController *result = nil;
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal) {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for (UIWindow *temp in windows) {
            if (temp.windowLevel == UIWindowLevelNormal) {
                window = temp;
                break;
            }
        }
    }
    //取当前展示的控制器
    result = window.rootViewController;
    while (1)
    {
        //根据不同的页面切换方式，逐步取得最上层的viewController
        if ([result isKindOfClass:[UITabBarController class]]) {
            result = ((UITabBarController*)result).selectedViewController;
        } else if ([result isKindOfClass:[UINavigationController class]]) {
            result = ((UINavigationController*)result).visibleViewController;
        }else if (result.presentedViewController) {
            result = result.presentedViewController;
        }else{
            break;
        }
    }

    return result;
    
}
@end
